/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.barang;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.sadhar.errhandler.ClassAntiNull;
import org.sadhar.sia.common.ClassConnection;
import org.springframework.jdbc.core.RowMapper;

/**
 *
 * @author hendro
 */
public class BarangDAOImpl implements BarangDAO {

    public List<Barang> list(int idJenisBarang, String namaBarang) throws Exception {
        List<Barang> list = new ArrayList<Barang>();
        String sql = "SELECT idBarang, kodeBarang, namaBarang, idJenisBarang, kodeSatuanBarang, kodeUnitPenginput " +
                "FROM asset.barang WHERE idJenisBarang=? AND namaBarang LIKE ?";
        try {
            List<Map> rows = ClassConnection.getJdbc().queryForList(sql, new Object[]{
                        idJenisBarang,
                        "%" + namaBarang + "%"
                    });
            for (Map m : rows) {
                Barang brg = new Barang();
                brg.setIdBarang(ClassAntiNull.AntiNullInt(m.get("idBarang")));
                brg.setKodeBarang(ClassAntiNull.AntiNullString(m.get("kodeBarang")));
                brg.setNamaBarang(ClassAntiNull.AntiNullString(m.get("namaBarang")));
                brg.setIdJenisBarang(ClassAntiNull.AntiNullInt(m.get("idJenisBarang")));
                brg.setKodeSatuanBarang(ClassAntiNull.AntiNullString(m.get("kodeSatuanBarang")));
                brg.setKodeUnitPenginput(ClassAntiNull.AntiNullString(m.get("kodeUnitPenginput")));
                list.add(brg);
            }
            return list;
        } catch (Exception ex) {
            return new ArrayList();
        }

    }

    public Barang select(int id) throws Exception {
        String sql = "SELECT idBarang, kodeBarang, namaBarang, idJenisBarang, kodeSatuanBarang, kodeUnitPenginput " +
                "FROM asset.barang WHERE idBarang=?";
        return (Barang) ClassConnection.getJdbc().queryForObject(sql, new Object[]{
                    id
                }, new RowMapper() {

            public Object mapRow(ResultSet rs, int arg1) throws SQLException {
                Barang brg = new Barang();
                brg.setIdBarang(rs.getInt("idBarang"));
                brg.setKodeBarang(rs.getString("kodeBarang"));
                brg.setNamaBarang(rs.getString("namaBarang"));
                brg.setIdJenisBarang(rs.getInt("idJenisBarang"));
                brg.setKodeSatuanBarang(rs.getString("kodeSatuanBarang"));
                brg.setKodeUnitPenginput(rs.getString("kodeUnitPenginput"));
                return brg;
            }
        });
    }
}
